package com.wwwarehouse.usercenter.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;

import com.wwwarehouse.usercenter.R;
import com.wwwarehouse.usercenter.bean.authoritydistribute.AuthorityWrapItemBean;
import com.wwwarehouse.usercenter.bean.business_relationship.BRCFDTagsBean;
import com.wwwarehouse.usercenter.bean.business_relationship.BRCustomerFileDetailBean;

import java.util.ArrayList;
import java.util.List;

/**
 * @author : mundane
 * @time : 2017/9/20 14:14
 * @description :
 * @file : AuthorityDistributeRvAdapter.java
 */
public class BRCFDTagsAdapter extends RecyclerView.Adapter<BRCFDTagsAdapter.BRCFDTagsAdapterHolder> {

	private final int mRvHeight;
	private BRCFDTagsBean mDataList;
	private Context mContext;
	private ArrayList<BRCustomerFileDetailBean.TagsBean> tagIds;

	public interface OnItemClickListener{
		void onButtonClicked(int position);

	}

	private OnItemClickListener mOnItemClickListener;

	public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
		mOnItemClickListener = onItemClickListener;
	}

	public BRCFDTagsAdapter(BRCFDTagsBean dataList, int height,ArrayList<BRCustomerFileDetailBean.TagsBean> tagIds) {
		this.mDataList = dataList;
		mRvHeight = height;
		this.tagIds = tagIds;
	}

	@Override
	public BRCFDTagsAdapterHolder onCreateViewHolder(ViewGroup parent, int viewType) {
		mContext = parent.getContext();
		View view = LayoutInflater.from(mContext).inflate(R.layout.item_rv_authority_distribute, parent, false);
		return new BRCFDTagsAdapterHolder(view);
	}

	@Override
	public void onBindViewHolder(final BRCFDTagsAdapterHolder holder, final int position) {
		ViewGroup.LayoutParams params = holder.itemView.getLayoutParams();
		params.height = (int) (mRvHeight * 0.85 / 9);
		holder.itemView.setLayoutParams(params);
		BRCFDTagsBean.ListBean listBean = mDataList.getList().get(position);
		boolean isSelected = false;
		for(BRCustomerFileDetailBean.TagsBean tagsBean : tagIds){
			if (tagsBean.getTagId().equals(listBean.getTagUkid()))
				isSelected = true;
		}
		if (isSelected) {
            holder.mBtn.setBackgroundResource(R.drawable.shape_corner_blue);
			holder.mBtn.setTextColor(mContext.getResources().getColor(R.color.common_color_c1_337cff));
		} else {
			holder.mBtn.setBackgroundResource(R.drawable.shape_corner_grey);
			holder.mBtn.setTextColor(mContext.getResources().getColor(R.color.common_color_c7_96999e));
		}
		holder.mBtn.setText(listBean.getTagName());
		if (mOnItemClickListener != null) {
			holder.mBtn.setOnClickListener(new View.OnClickListener() {
				@Override
				public void onClick(View v) {
					mOnItemClickListener.onButtonClicked(position);
				}
			});
		}
	}

	@Override
	public int getItemCount() {
		return mDataList == null ? 0 : mDataList.getList().size();
	}

	static class BRCFDTagsAdapterHolder extends RecyclerView.ViewHolder {

		public Button mBtn;

		public BRCFDTagsAdapterHolder(View itemView) {
			super(itemView);
			mBtn = (Button) itemView.findViewById(R.id.btn);
		}
	}
}
